﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Globalization;

namespace hozoroghiab
{
    public partial class holidays : Form
    {
        public sql sqlquery;
        int[] month = new int[12] { 31, 31, 31, 31, 31, 31, 30, 30, 30, 30, 30, 29 };
        DataGridViewButtonColumn editeButton;
        public holidays(sql s)
        {
            InitializeComponent();
            sqlquery = s;
        }

        private void holidays_Load(object sender, EventArgs e)
        {
            dataGridView1.ReadOnly = true;
            dataGridView1.AllowUserToAddRows = false;
            PersianCalendar per = new PersianCalendar();
            string year = per.GetYear(DateTime.Now).ToString();
            string month = per.GetMonth(DateTime.Now).ToString();
            setdatagridview(year, month);
            yeartoolStripTextBox.Text = year;
            monthtoolStripComboBox1.Text = month;
            dataGridView1.Columns[0].Width = 60;
            dataGridView1.Columns[1].Width = 60;
            dataGridView1.Columns[2].Width = 60;
            dataGridView1.Columns[3].Width = 60;
            dataGridView1.Columns[4].Width = 60;
            dataGridView1.Columns[5].Width = 60;
            editeButton = new DataGridViewButtonColumn();
            editeButton.HeaderText = "ویرایش";
            editeButton.Text = "ویرایش";
            editeButton.UseColumnTextForButtonValue = true;
            editeButton.Width = 80;
            dataGridView1.Columns.Add(editeButton);
        }
        void setdatagridview(string year, string month)
        {
            sqlquery.open();
            int count = (int)sqlquery.ExecuteScalar("SELECT COUNT(*) FROM days where year=" + year + " and month=" + month + "");
            PersianCalendar per = new PersianCalendar();
            int[] monthcopy = new int[12];
            Array.Copy(this.month, monthcopy, this.month.Length);
            if (per.IsLeapYear(Convert.ToInt32(year)))
            {
                monthcopy[11] = 30;
            }

            if (count != monthcopy[Convert.ToInt16(month) - 1])
            {
                for (int i = 1; i <= monthcopy[Convert.ToInt16(month) - 1]; i++)
                {
                    sqlquery.ExecuteNonQuery("INSERT INTO days (year,month,day,beginhour,beginmin,endhour,endmin,holiday)VALUES(" + year + "," + month + "," + i + ",7,30,15,30,0)");
                }


               /* string value = "";
                for (int i = 1; i <= monthcopy[Convert.ToInt16(month) - 1]; i++)
                {
                    value += "(" + year + "," + month + "," + i + "),";
                }
                sqlquery.ExecuteNonQuery("INSERT INTO days (year,month,day)VALUES" + value.Substring(0, value.Length - 1));
                //sqlserver 2008
                */
            }
            SqlDataAdapter da = sqlquery.sqlcommand("SELECT year as [سال],month as [ماه],day as [روز],holiday as[تعطیل],ltrim(str(beginhour))+':'+ltrim(str(beginmin)) as [زمان شروع],ltrim(str(endhour))+':'+ltrim(str(endmin)) as [زمان پایان] FROM days where year=" + year + " and month=" + month + "");
            DataTable table = new DataTable();
            da.Fill(table);
            dataGridView1.DataSource = table;
            sqlquery.close();
        }


        private void  toolStripButton1_Click(object sender, EventArgs e)
        {
            setdatagridview(yeartoolStripTextBox.Text.Trim(), monthtoolStripComboBox1.Text.Trim());
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            int currentRow = int.Parse(e.RowIndex.ToString());
            if (dataGridView1.Columns[e.ColumnIndex] == editeButton && currentRow >= 0)
            {
                editedays edforms = new editedays(sqlquery, dataGridView1[0, currentRow].Value.ToString(), dataGridView1[1, currentRow].Value.ToString(), dataGridView1[2, currentRow].Value.ToString(), dataGridView1[3, currentRow].Value.ToString(), dataGridView1[4, currentRow].Value.ToString(), dataGridView1[5, currentRow].Value.ToString());
                edforms.Show();
            }
        }

        

    }
}
